package com.youlu.campus.service.ca.impl;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.UserRecieveCaRecord;
import com.youlu.campus.service.ca.UserRecieveCaRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;


@Slf4j
@Service
public class UserRecieveCaRecordServiceImpl implements UserRecieveCaRecordService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean exist(String userId, String activityId, String level) {
        log.info(":>>> userId:{},activityId:{},level:{} ", userId, activityId, level);
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(activityId) || StringUtils.isBlank(level)) {
            log.error(":>>> 参数错误");
            throw new BusinessException("参数错误");
        }
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("activityId").is(activityId).and("level").is(level));
        boolean exist = mongoTemplate.exists(query, UserRecieveCaRecord.class);
        return exist;
    }

    @Override
    public boolean create(UserRecieveCaRecord record) {
        record.setId(null);
        record.setCreatedTime(new Date());
        mongoTemplate.insert(record);
        return true;
    }
}
